﻿namespace Busconeitor.Web.Controllers
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Web.Mvc;
    using System.Linq;
    using Busconeitor.Web.Models;

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Busconeitor";

            return this.View();
        }

        public ActionResult About()
        {
            return this.View();
        }

        [HttpPost]
        public ActionResult Search(string query, int? page)
        {
            if (!string.IsNullOrEmpty(query))
            {
                var model = new QueryModel { Query = query };
                model.Results = Paginacion(page);

                return this.View(model);
            }

            return this.RedirectToAction("Index", "Home");
        }

        [HttpGet]
        public ActionResult GetPage(string query, int? page)
        {
            var model = new QueryModel { Query = query };
            model.Results = Paginacion(page);

            return this.PartialView("_Results", model);
        }

        private List<ResultModel> ObtenerTodos()
        {
            var list = new List<ResultModel>();

            for (int i = 1; i <= 50; i++)
            {
                var item = new ResultModel { Descripcion = "Resultado " + i };
                list.Add(item);
            }

            return list;
        }

        private List<ResultModel> Paginacion(int? page)
        {
            const int pageSize = 10;
            var pagina = ObtenerTodos().Skip((page ?? 0) * pageSize).Take(pageSize).ToList();

            return pagina;
        }
    }
}
